﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PessMvc.Helpers;
using DoddleReport;
using DoddleReport.Web;
using System.Web.Security;
namespace PessMvc.Controllers
{
    [Authorize]
    public class CommentController : Controller
    {
        //
        // GET: /Comment/
        PessModel.PESSEntities db;
        public CommentController()
        {
            db = new PessModel.PESSEntities();
        }
        [Authorize(Roles="Admin,ExpertJudge")]
        public ActionResult Export2Excel(int expertGroupID)
        {
            var expertGroup = db.ExpertGroups.Find(expertGroupID);
            if(User.IsInRole("ExpertJudge")&&expertGroup.LeaderID!=null&&expertGroup.LeaderID.Trim().ToLower()== Membership.GetUser().UserName.Trim().ToLower())
            {
                new Exception("非评委组长");
            }
            var query = (from a in expertGroup.Archives
                    join c in db.ExpertComments on a.ID equals c.ArchiveID
                    join e in db.ExpertJudges on c.ExpertSerialNumber.ToLower() equals e.SerialNumber.ToLower()
                         select new { ArchiveSN = a.SerialNumber, AuthorName = a.TeacherName, c.Comment, ExpertJudge = e.RealName })//c.ExpertJudge.RealName })
                    .AsEnumerable();

            //throw new Exception("subTitle failed to generaye because FK between ExpertGroup and Subject has been removed");
            //string subTitle = "CommentController.Export2Excel";
            string subTitle = string.Format("{0}-{1}",
                expertGroup.Callsign,
                expertGroup.Activity.Name
                );
            var report = Utility.CreateReport(query,null, "评语汇总", subTitle);
            IReportWriter writer = new DoddleReport.Writers.ExcelReportWriter();
            return new ReportResult(report, writer, "application/vnd.ms-excel") { FileName = HttpUtility.UrlPathEncode(subTitle+".xls") }; 
        }

        [Authorize(Roles = "Admin,ExpertJudge")]
        public ActionResult ExportExpertResponses2Excel(int expertGroupID)
        {
            var expertGroup = db.ExpertGroups.Find(expertGroupID);
            if (User.IsInRole("ExpertJudge") && expertGroup.LeaderID != null && expertGroup.LeaderID.Trim().ToLower() == Membership.GetUser().UserName.Trim().ToLower())
            {
                new Exception("非评委组长");
            }

            //Compared with function Export2Excel, only the following query is different. Consider eliminate redundant code in the future. Aut/25/2017
            var query = (from a in expertGroup.Archives
                         join r in db.Responses on a.ID equals r.ArchiveID
                         join e in db.ExpertJudges on r.ExpertSerialNumber.ToLower() equals e.SerialNumber.ToLower()
                         join s in db.Standards on r.StandardID equals s.ID
                         select new { ArchiveSN = a.SerialNumber, AuthorName = a.TeacherName,s.Title, r.Comment, ExpertJudge = e.RealName })//c.ExpertJudge.RealName })
                    .AsEnumerable();

            string subTitle = string.Format("{0}-{1}",
                expertGroup.Callsign,
                expertGroup.Activity.Name
                );
            var report = Utility.CreateReport(query, null, "分项评语汇总", subTitle);
            IReportWriter writer = new DoddleReport.Writers.ExcelReportWriter();
            return new ReportResult(report, writer, "application/vnd.ms-excel") { FileName = HttpUtility.UrlPathEncode(subTitle + ".xls") };
        }
    }
}
